<template>
   
  <div class="pagination">
    <button :disabled="pageNo === 1">上一页</button>
    <button v-if="startEnd.start > 1">1</button>
    <!-- 大于1就是2的时候出现1 -->
    <button v-if="startEnd.start > 2">···</button>
    <!-- 如果大于2就是3的时候会出现... -->

    <button
      :class="{ active: pageNo === startEnd.start + num - 1 }"
      v-for="num in startEnd.end - startEnd.start + 1"
      :key="num"
    >
      {{ startEnd.start + num - 1 }}
    </button>

    <button v-if="startEnd.end < totalPage - 1">···</button>
    <button v-if="startEnd.end < totalPage">{{ totalPage }}</button>
    <button :disabled="pageNo === totalPage">下一页</button>

    <button style="margin-left: 30px">共 {{ total }} 条</button>
  </div>
  <!-- 
            当前页(pageNo)       正在看第几页
            总数(total)          一共多少个商品
            连续页码(continues)  中间连续按钮的个数 通常都是奇数 为了对称好看
            页大小(pageSize)     一页展示多少个商品

            总页数(totalPage)   需要计算 向上取整(总数/页大小)
           -->
</template>



<script>
export default {
  name: "Pagination",
  props: {
    // 总数
    total: Number,
    // 当前页
    pageNo: {
      type: Number,
      default: 5,
    },
    // 连续页
    continues: {
      type: Number,
      required: true,
    },
    // 每页页数
    pageSize: Number,
  },
  computed: {
    // 计算总页数
    totalPage() {
      return Math.ceil(this.total / this.pageSize);
    },
    // 计算开始和结束
    startEnd() {
      let { pageNo, continues, totalPage } = this;
      let start;
      let end;
      // 如果总页数小于连续页
      if (totalPage < continues) {
        start = 1;
        end = totalPage;
      } else {
        //如果总页数大于连续页
        start = pageNo - (continues - 1) / 2;
        end = pageNo + (continues - 1) / 2;
        if (start < 1) {
          // 如果起始页小于1
          start = 1;
          end = continues;
        }
        if (end > totalPage) {
          // 如果结束页大于总页
          start = totalPage - continues + 1;
          end = totalPage;
        }
      }
      return { start, end };
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;

  button {
    margin: 0 5px;

    background-color: #f4f4f5;

    color: #606266;

    outline: none;

    border-radius: 2px;

    padding: 0 4px;

    vertical-align: top;

    display: inline-block;

    font-size: 13px;

    min-width: 35.5px;

    height: 28px;

    line-height: 28px;

    cursor: pointer;

    box-sizing: border-box;

    text-align: center;

    border: 0;

    &[disabled] {
      color: #c0c4cc;

      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;

      background-color: #409eff;

      color: #fff;
    }
  }
}
</style>
